import { $login } from '@/api/index';
import router from '@/router';
import { Message } from 'element-ui';
const user = {
    state: {
        account: "",
        username: "",
        type: "",
        token: ""
    },
    mutations: {
        SET_ACCOUNT(state, account) {
            state.account = account;
        },
        SET_USERNAME(state, username) {
            state.username = username;
        },
        SET_TYPE(state, type) {
            state.type = type;
        },
        SET_TOKEN(state, token) {
            state.token = token;
        }
    },
    actions: {
        LoginAction({ commit }, userInfo) {
            $login(userInfo).then(res => {
                console.log(res);
                if (res.data.msg == 'success') {
                    commit('SET_ACCOUNT', res.data.data.account);
                    commit('SET_USERNAME', res.data.data.username);
                    commit('SET_TYPE', res.data.data.type);
                    commit('SET_TOKEN', res.data.token);
                    router.push('/home/dashboard')
                    Message({ message: "登录成功", type: "success" })
                }
            })
        },
        QuitAction(context) {
            context.commit('SET_ACCOUNT', "");
            context.commit('SET_USERNAME', "");
            context.commit('SET_TYPE', "");
            context.commit('SET_TOKEN', "");
            router.push('/login');
            Message("已注销");
        }
    }
}
export default user;